/*
 * @Author: ૮₍ ˙О˙₎ა
 * @Date: 2023-08-10 14:21:18
 */
import { createSlice } from "@reduxjs/toolkit";
import type { reqAttrCategory } from '@/api/attr/attrType'
import { reqGetCategory1, reqGetCategory2, reqGetCategory3 } from "@/api/attr/attr";
interface categoryStore {
  category1Id: string | number
  category2Id: string | number
  category3Id: string | number
  category1List: reqAttrCategory[]
  category2List: reqAttrCategory[]
  category3List: reqAttrCategory[]
}

// 初始化状态
let category: categoryStore = {
  category1Id: '',
  category2Id: '',
  category3Id: '',
  category1List: [],
  category2List: [],
  category3List: []
}
// 创建redux模块
export const categorySlice = createSlice({
  name: "category",
  // 初始化状态数据
  initialState:category,
  // reducer函数 --> 会自动生成同步action函数
  reducers: {
    setCategory1Id(state, {payload}) {
      state.category1Id=payload
    },

    setCategory2Id(state, {payload}) {
      state.category2Id=payload
    },

    setCategory3Id(state, {payload}) {
      state.category3Id=payload
    },
    getCategory1List(state, {payload}) {
      state.category1List=payload
    },

    getCategory2List(state, {payload}) {
      state.category2List=payload
    },

    getCategory3List(state, {payload}) {
      state.category3List=payload
    }
  },
});
// 暴露同步action
export const { getCategory1List,getCategory2List,getCategory3List,setCategory1Id,setCategory2Id,setCategory3Id } = categorySlice.actions;

export let getAsyncCategory1List=() =>{
  return async (dispatch:any)=>{
    const res = await reqGetCategory1()
    dispatch(getCategory1List(res.data))
  }
}
export let getAsyncCategory2List=(id:number) =>{
  return async (dispatch:any)=>{
    const res = await reqGetCategory2(id)
    dispatch(getCategory2List(res.data))
    return Promise.resolve(res.data)
  }
}
export let getAsyncCategory3List=(id:number) =>{
  return async (dispatch:any)=>{
    const res = await reqGetCategory3(id)
    dispatch(getCategory3List(res.data))
    return Promise.resolve(res.data)
  }
}



export default categorySlice.reducer;